package com.service.impl;

import java.util.List;

import com.dao.impl.SchoolDao;
import com.dao.impl.StudentDao;
import com.entity.School;
import com.entity.Student;
import com.service.IStudentService;
import com.tools.Page;

/**
 * 学生的业务
 * 
 * @author Administrator
 *
 */
public class StudentService implements IStudentService {
	private StudentDao studentDao;
	private SchoolDao schoolDao;

	/*
	 * (non-Javadoc)
	 * 
	 * @see com.service.IStudentService#add(com.entity.Student)
	 */
	@Override
	public boolean add(Student student) {
		studentDao = new StudentDao(0);
		try {
			boolean save = studentDao.save(student);
			studentDao.commit();
			return save;
		} catch (Exception e) {
			e.printStackTrace();
			studentDao.rollback();
		} finally {
			studentDao.close();
		}

		return false;

	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see com.service.IStudentService#update(com.entity.Student)
	 */
	@Override
	public boolean update(Student student) {
		studentDao = new StudentDao(0);
		try {
			boolean update = studentDao.update(student);
			studentDao.commit();
			return update;
		} catch (Exception e) {
			e.printStackTrace();
			studentDao.rollback();
		} finally {
			studentDao.close();
		}

		return false;

	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see com.service.IStudentService#delete(com.entity.Student)
	 */
	@Override
	public boolean delete(Student student) {
		studentDao = new StudentDao(0);
		try {
			boolean delete = studentDao.deleteById(student.getId());
			studentDao.commit();
			return delete;
		} catch (Exception e) {
			e.printStackTrace();
			studentDao.rollback();
		} finally {
			studentDao.close();
		}

		return false;

	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see com.service.IStudentService#findById(java.lang.String)
	 */
	@Override
	public Student findById(String id) {
		studentDao = new StudentDao(0);
		schoolDao = new SchoolDao(1);
		schoolDao.setCon(studentDao.getCon());
		try {
			Student findById = studentDao.findById(id);
			// 补充学校的信息
			School findById2 = schoolDao.findById(findById.getSchool().getId());
			findById.setSchool(findById2);
			studentDao.commit();
			return findById;
		} catch (Exception e) {
			e.printStackTrace();
			studentDao.rollback();
		} finally {
			studentDao.close();
			schoolDao.close();
		}

		return null;

	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see com.service.IStudentService#getList(com.tools.Page)
	 */
	@Override
	public List<Student> getList(Page page) {
		studentDao = new StudentDao(0);
		schoolDao = new SchoolDao(1);
		schoolDao.setCon(studentDao.getCon());
		try {
			// 获得总记录
			page.setCountSize(studentDao.count());
			List<Student> list = studentDao.getList(page);
			for (int i = 0; i < list.size(); i++) {
				// 补充学校的信息
				School findById2 = schoolDao.findById(list.get(i).getSchool().getId());
				list.get(i).setSchool(findById2);
			}
			studentDao.commit();
			return list;
		} catch (Exception e) {
			e.printStackTrace();
			studentDao.rollback();
		} finally {
			studentDao.close();
			schoolDao.close();
		}

		return null;

	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see com.service.IStudentService#count(com.tools.Page)
	 */
	@Override
	public void count(Page page) {
		studentDao = new StudentDao(0);
		try {
			// 获得总记录
			page.setCountSize(studentDao.count());
			studentDao.commit();
		} catch (Exception e) {
			e.printStackTrace();
			studentDao.rollback();
		} finally {
			studentDao.close();
		}

	}

}
